 /**
 * @author caif
 * @fileoverview login module
 */
	
$(function(){
	if($('#inactiveUserId').val()){
		location.href=location.href.replace('/login?','/register?');
	}
	
	$('#glodon_fwd_dlg').modal({
		keyboard: true,
		show: false,
		backdrop: "static"
	});
	
	var user = {
		onCorrect: function(ele){
			$(ele).next("span").html("<img src="+ $(ele).next("span").attr("data-img") +">");
		},
		
		updateMsg : function( selector , msg ){
			$(selector).html($(selector).attr(msg));
		},
		
		initialize : function(){
			var that = this;
			
			$("#glodon_submit a:first").click(function(event){
				if( !$("#userName_str").val() || !$("#password_str").val()){
					if( !$("#userName_str").val() ){
						that.updateMsg("#userName_str +span" , "data-msg");
						$("#userName_str").parents(".control-group").addClass("error");
						$("#userName_str").focus();
					}else if(!$("#userName_str").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)){
						that.updateMsg("#userName_str +span" , "data-cmsg");
					}
						
					if( !$("#password_str").val() ){
						that.updateMsg("#password_str +span" , "data-msg");
						$("#password_str").parents(".control-group").addClass("error");
						$("#password_str").focus();
					}
					return false;
				}else if( $("#userName_str +span").text() ){
					$("#userName_str").focus();
					return false;
				}else{
					$("#glodon_submit a:first").button("loading");
					$("form#glodon_login").submit();
				}
			});
			
			$("#glodon_email").blur(function(){
		    	if( !$(this).val() ){
		    		that.updateMsg("#glodon_email +span" , "data-msg");
				}else if(!$("#glodon_email").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)){
		    		that.updateMsg("#glodon_email +span" , "data-cmsg");
	    		}else{
				    $.post("/account/check.json",{loginName :$("#glodon_email").val() } , function(data){
			            if(eval(data.result)){
				            that.onCorrect($("#glodon_email"));
			            }else{
			                that.updateMsg("#glodon_email +span" , "data-hmsg");
			            }
			        } ,"json");
				}
			});
			
			var timestamp = Math.round(new Date().getTime()/1000);
			$("#captcha").data({timestamp : timestamp });
			
			$("#captcha").html("<img src=captcha?timestamp="+ $("#captcha").data("timestamp") + "/>");
			
			$("#vcode").blur(function(){
			    if( !$(this).val() ){
		    		that.updateMsg("#verify_code .glodon_error" , "data-msg");
				}else{
				    $.post("captcha/verify.json",{ captchaValue : $("#vcode").val()  } , function(data){
			            if(eval(data.result)){
				            that.onCorrect($("#paas_login_change"));
			            }else{
			                that.updateMsg("#verify_code .glodon_error" , "data-cmsg");
			            }
			        }
			        ,"json"
			        )
				}
			})
			
			$("#paas_login_change").click(function(){
			    $("#captcha").data({timestamp : Math.round(new Date().getTime()/1000) });
			    $("#captcha").html("<img src=captcha?timestamp="+ $("#captcha").data("timestamp") + "/>");
			    $("#paas_login_change").next("span").html("");
			})
			
			$("#glodon_password").change(function(){
		    	if(!$(this).val()){
		    		that.updateMsg("#glodon_password +span" , "data-msg");
				}else{
		    		that.onCorrect(this);
				}
			});
			
			$("#glodon_password_confirm").change(function(){
				if( !$(this).val() ){
					that.updateMsg("#glodon_password_confirm +span" , "data-msg" );
				}else if( $("#glodon_password").val() != $(this).val() ){
		    		that.updateMsg("#glodon_password_confirm +span" , "data-cmsg" );
				}else{
					that.onCorrect(this);;
				}
			});
			
			
			$("#userName_str").change(function(){
		    	if( !$(this).val()){
		    		that.updateMsg("#userName_str +span" , "data-msg");
				}else if(!$("#userName_str").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)){
		    		that.updateMsg("#userName_str +span" , "data-cmsg");
	    		}else{
		    		that.onCorrect(this);;
				}
			});
			
			$("#password_str").change(function(){
		    	if(!$(this).val()){
		    		that.updateMsg("#password_str +span" , "data-msg");
				}else{
		    		that.onCorrect(this);;
				}
			});
			
			$("#glodon_register_container input").keypress(function(e){
			    if(e.keyCode === 13){
			        $("#glodon_register_submit a").trigger("click");
			    }
			})
			
			$("#glodon_login_container input").keypress(function(e){
			    if(e.keyCode === 13){
			        $("#glodon_submit a:first").trigger("click");
			    }
			})
			
			$("#glodon_register_submit a").click(function(e){
				var flag = true;
				$("#glodon_register_container .glodon_error").each(function(){
					if( $(this).text().trim()){
						$(this).parents(".control-group").addClass("error");
						flag = false;
					}
				});
				
				$("#glodon_register_container input").each(function(){
					if( !$(this).val()){
						var text = $(this).next().attr('data-msg');
						$(this).parents(".control-group").addClass("error");
						$(this).next().html(text);
						flag = false;
					}
				});
				
				if(flag){
					$("#register_form").submit( );
				}
				return false;
			})
			
			$("#register_form input").change(function(){
				$(this).parents(".control-group").removeClass("error");
			})
			$("#glodon_login input").change(function(){
				$(this).parents(".control-group").removeClass("error");
			})
			
			
			if($("#glodon_login_error").hasClass("login_error")){
				$("#notify").addClass("fail");
				$("#notify-msg").html("用户名密码不匹配");
				$("#mask").hide();
				$("#notify-wrapper").fadeIn(1).delay(2000).fadeOut(800 );
			}
			
			$(".fPassword").click(function(e){
			    e.preventDefault();
			    $("#resetFormMsg").empty();
			    $("#email_success").hide();
        	    $("#sendEmail").show();
			})
			
			$("#resetForm button[type='submit']").click(function(e){
			    e.preventDefault();
			    if( !$("#resetEmail").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) ){
			        $("#resetEmail").parents(".control-group").addClass("error");
			    }else{
			        $("#mask").show();
			        $("#resetForm").ajaxSubmit({
        				url:"/account/emailResetPasswordLink.json",
        				type:"post",
        				success : function(data){
        				    $("#mask").hide();
        				    if( data.result == "fail" ){
        				        $("#resetFormMsg").html($("#resetFormMsg").attr("data-fmsg")).css({color:"red"});
        				    }else if(!eval(data.result)){
        				        $("#resetFormMsg").html($("#resetFormMsg").attr("data-msg")).css({color:"red"});
        				    }else{
        				        
        				        $("#email_success").show();
        				        $("#sendEmail").hide();
        				    }
        				}
        			});
			    }
			})
			$("#resetEmail").change(function(){
			    $(this).parents(".control-group").removeClass("error");
			})
			
			$("#forPWD input").change(function(){
			    $(this).parents(".control-group").removeClass("error");
			})
			
			$("#forPWD button[type='submit']").click(function(e){
			    e.preventDefault();
			    var flag = true;
				$("#forPWD .glodon_error").each(function(){
					if( $(this).text().trim()){
						$(this).parents(".control-group").addClass("error");
						flag = false;
					}
				});
				
				$("#forPWD input").each(function(){
					if( !$(this).val() ){
						var text = $(this).next().attr('data-msg');
						$(this).parents(".control-group").addClass("error");
						$(this).next().html(text);
						flag = false;
					}
				});
				
			    if( flag ){
			        $("#mask").show();
			        $("#forPWD form").ajaxSubmit({
        				url:"/account/changePassword.json",
        				type:"post",
        				success : function(data){
        				    $("#mask").hide();
        				    if(eval(data.result)){
            				    $("#forPWD").hide(); 
            				    $("#sucPWD").show();
            				    (function jump(count) {    
                                    window.setTimeout(function(){    
                                        count--;    
                                        if(count > 0) {    
                                            $('#count').text( count );    
                                            jump(count);    
                                        } else {    
                                            location.href=window._global["contextPath"];    
                                        }    
                                    }, 1000);    
                                })(parseInt($("#count").text()));   
        				    }else{
        				        $("#forPWD").hide(); 
        				        $("#failPWD").show();
        				        (function jump(count) {    
                                    window.setTimeout(function(){    
                                        count--;    
                                        if(count > 0) {    
                                            $('#fcount').text( count );    
                                            jump(count);    
                                        } else {    
                                            location.href=window._global["contextPath"];    
                                        }    
                                    }, 1000);    
                                })(parseInt($("#fcount").text()));   
        				    }
        				    
        				}
        			});
			    }
			})
			
			$("#npassword").change(function(){
		    	if( !$(this).val() ){
		    		that.updateMsg("#npassword +span" , "data-msg");
				}else{
		    		that.onCorrect(this);
				}
			});
			
			$("#cpassword").change(function(){
				if( !$(this).val() ){
					that.updateMsg("#cpassword +span" , "data-msg" );
				}else if( $("#npassword").val() != $(this).val() ){
		    		that.updateMsg("#cpassword +span" , "data-cmsg" );
				}else{
					that.onCorrect(this);;
				}
			});
		}
	}
	user.initialize();
})
